<template>
  <view class="my-container">
    <view class="header-bg">
      <image class="bg-image" src="/static/wine-cabinet.png" mode="aspectFill"></image>
    </view>
    <view class="user-info">
      <image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'" mode="aspectFill"></image>
      <text class="username">{{userInfo.nickname || '微信用户'}}</text>
    </view>
    <view class="menu-list">
      <view class="menu-item" v-for="(item, index) in menuItems" :key="index" @click="handleMenuClick(item.type)">
        <view class="menu-icon">
          <image :src="item.icon" mode="aspectFit"></image>
        </view>
        <text class="menu-text">{{item.text}}</text>
        <view class="arrow-right">
          <image src="/static/right.png" mode="aspectFit"></image>
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
import { getUserInfo } from '@/utils/wechat.js';
import { getCurrentUserInfo } from '@/api/user.js';

export default {
  data() {
    return {
      userInfo: {},
      menuItems: [
        { icon: '/static/user-info.png', text: '个人信息', type: 'userInfo' },
        { icon: '/static/my-favorite.png', text: '我的收藏', type: 'favorite' },
        { icon: '/static/feedback.png', text: '意见反馈', type: 'feedback' },
        { icon: '/static/settings.png', text: '设置', type: 'settings' }
      ]
    }
  },
  onShow() {
    // 获取最新的用户信息
    this.fetchUserInfo();
  },
  methods: {
    // 获取用户信息
    fetchUserInfo() {
      // 先从本地获取缓存的用户信息
      const cachedUserInfo = getUserInfo();
      if (cachedUserInfo) {
        this.userInfo = cachedUserInfo;
      }
      
      // 从服务器获取最新用户信息
      getCurrentUserInfo().then(res => {
        if (res.code === '00000' && res.data) {
          this.userInfo = res.data;
          // 更新本地缓存
          uni.setStorageSync('user_info', res.data);
        }
      }).catch(err => {
        console.error('获取用户信息失败', err);
      });
    },
    handleMenuClick(type) {
      switch (type) {
        case 'userInfo':
          uni.navigateTo({
            url: '/pages/user-info/user-info'
          });
          break;
        case 'favorite':
          uni.switchTab({
            url: '/pages/favorite/favorite'
          });
          break;
        case 'feedback':
          uni.navigateTo({
            url: '/pages/feedback/feedback'
          });
          break;
        case 'settings':
          uni.navigateTo({
            url: '/pages/settings/settings'
          });
          break;
        default:
          console.log('未知的菜单类型：', type);
      }
    },
    // 跳转到扫码页面
    goToScan() {
      uni.navigateTo({
        url: '/pages/scan/scan'
      });
    }
  }
}
</script>

<style lang="scss">
.my-container {
  min-height: 100vh;
  background-color: #fff;
  position: relative;
}

.header-bg {
  height: 50vh;
  width: 100%;
  position: relative;
  
  .bg-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.user-info {
  position: absolute;
  top: 340rpx;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  
  .avatar {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    border: 4rpx solid #fff;
    box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
  }
  
  .username {
    margin-top: 20rpx;
    font-size: 32rpx;
    color: #fff;
    font-weight: 500;
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
  }
}

.menu-list {
  position: relative;
  margin: -100rpx 30rpx 0;
  background-color: #fff;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  z-index: 1;
  
  .menu-item {
    display: flex;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid #f5f5f5;
    transition: all 0.3s ease;
    
    &:active {
      background-color: #f9f9f9;
      transform: translateY(2rpx);
    }
    
    &:last-child {
      border-bottom: none;
    }
    
    .menu-icon {
      width: 40rpx;
      height: 40rpx;
      margin-right: 20rpx;
      
      image {
        width: 100%;
        height: 100%;
      }
    }
    
    .menu-text {
      flex: 1;
      font-size: 28rpx;
      color: #333;
    }
    
    .arrow-right {
      width: 32rpx;
      height: 32rpx;
      
      image {
        width: 100%;
        height: 100%;
        opacity: 0.6;
      }
    }
  }
}

/* 扫一扫按钮样式 */
.scan-button {
  position: fixed;
  right: 40rpx;
  bottom: 120rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 10;
  
  .scan-icon {
    width: 100rpx;
    height: 100rpx;
    background-color: #8B0000;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4rpx 16rpx rgba(139, 0, 0, 0.3);
    margin-bottom: 10rpx;
    
    .icon-text {
      font-size: 50rpx;
      color: #fff;
    }
  }
  
  .scan-text {
    font-size: 24rpx;
    color: #333;
  }
}
</style>